package com.cloudwise.monitoring_system.service.impl;

import com.cloudwise.monitoring_system.entity.MonitorValue;
import com.cloudwise.monitoring_system.service.RedisService;
import com.cloudwise.monitoring_system.util.BussinessConstants;
import com.cloudwise.monitoring_system.util.MyRedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Reisen
 * @description:redis处理数据实现类
 * @date 2021/6/7 11:25
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Resource
    private MyRedisUtil myRedisUtil;

    @Override
    public List<MonitorValue> getLatestRedisData() {
        String redisResult= BussinessConstants.type.REDIS_RESULT;
        //从redis中取数据数据格式为 key1:value1,key2:value2, ...
        String redisValue = myRedisUtil.getValue(redisResult);
        //System.out.println(redisValue);
        String[] valueArr = redisValue.split(",");
        ArrayList<MonitorValue> monitorValues = new ArrayList<>();
        for (String s : valueArr) {
            String[] data = s.split("/");
            MonitorValue monitorValue = new MonitorValue();
            for (String v : data) {

                monitorValue.setMonitorName(data[0]);
                monitorValue.setMonitorValue(Double.valueOf(data[1]));
                monitorValue.setCreateTime(data[2]);
            }
            monitorValues.add(monitorValue);
        }
        //System.out.println(monitorValues);
        return monitorValues;
    }
}
